import Vue from "vue";
import VueRouter from "vue-router";
import routes from "./routes";
import store from "@/store";
import { Message } from "element-ui";

Vue.use(VueRouter)

// 配置路由
let router = new VueRouter({
    mode: 'history',
    // 路由路径信息
    routes,
    //路由滚动行为
    scrollBehavior(to, from, savedPosition) {
        // return 期望滚动到哪个的位置
        return {y:0};
    }
});

// 全局前置守卫
router.beforeEach((to, from, next) => {
    if (to.path === '/login'||to.path === '/reg') {
        next()
        return
    }
    const token = window.sessionStorage.getItem('user')
    //没有登录访问
    if (!token) {

        next('/login')
    }
    //有登录
    next();
})
export default router;

const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
